<?php

namespace app\msgapi\controller;

use think\facade\Filesystem;
use app\utils\Filesystem as UtilsFilesystem;

/**
 * 通用上传接口
 * Class Upload
 */
class Hsjson extends Base
{

    /**
     * 通用文件上传接口
     * @return \think\response\Json|void
     */
    public function upload()
    {
        $file = $this->request->file('hs_file');
        $maxWidth = input('w');     $maxHeight = input('h');    $dir = input('d', 'images');
        if ($file) {
            $url = '/storage/' . str_replace('\\', '/', Filesystem::putFile($dir, $file));
            if ($url) {
                $result = UtilsFilesystem::resizeImage($url, $maxWidth, $maxHeight);// 文件压缩、尺寸处理
            } else {
                $result = ['code' => 1, 'msg' => '上传失败，请重试'];
            }
            return json($result);
        }
    }

    /**JSSDK文件上传接口
     * @return \think\response\Json|void
     */
    public function jssdk_upload()
    {
        $file = $this->request->file('hs_file');
        $filename = $file->getOriginalName();
        $ext = $file->getOriginalExtension();
        if($ext!=='txt')return error_json('请上传txt格式文件');
        if($file){
            file_put_contents('./'.$filename,file_get_contents($file));
            return json(['code'=>0,'msg'=>'上传成功','url' => './'.$filename]);
        }
    }

}